import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import {toast} from 'nc-lightapp-front'
import getDisabledIndex from "./getDisabledIndex";
import updateSubButtonStatus from "./updateSubButtonStatus";
//hi_psndoc_deptchg/hi_psndoc_part/hi_psndoc_dimission取行上的pk_psnjob,其他的取pk_psndoc_sub
const mainTables = ['hi_psndoc_deptchg', 'hi_psndoc_part', 'hi_psndoc_dimission'];

export default function changeLine(record, index) {
    const {areaCode} = this.state;
    const {selectedPsn, orgVal, editTable, disabledAllArea} = this.props;
    const pkField = mainTables.indexOf(areaCode) > -1 ? 'pk_psnjob' : 'pk_psndoc_sub';
    const allRows = editTable.getAllRows(areaCode);
    const pk_sub = record.values[pkField].value;

    let postData = {
        pk_org: orgVal.refpk,
        pk_sub,
        pk_psnjob: selectedPsn
    };

    return proFetch({
        url: '/nccloud/hrtrn/partrds/PartRdsPartChgAction.do',
        body: postData,
    })
        .then((res) => {
            if (res.success) {
                editTable.addRow(areaCode, index + 1, true, res.data[areaCode].rows[0].values);
                const disabledIndex = getDisabledIndex(index + 1, allRows.length + 1);
                disabledIndex && editTable.setEditableRowByIndex(areaCode, disabledIndex, false);
                this.hideAction();
                disabledAllArea(true);
                updateSubButtonStatus.call(this, true);
                this.props.button.setButtonDisabled({
                    'delete': true,
                    'edit': true,
                    'change': true
                })
            }
        });
}
